package com.org.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.org.bean.setting.MaterialsInfo;
import com.org.bean.system.TunnelInfo;
import com.org.service.MaterialsService;
import com.org.service.TunnelService;

@Controller
@RequestMapping("/material")
public class MaterialsAction {
	/**
	 * 物资信息初始化，录入及展示
	 */
	@Autowired
	private MaterialsService materialsService;
	@Autowired
	private TunnelService tunnelService;
	private Iterator<String> iter;
	private List<String> list;

	private Map<String, Object> status(List<MaterialsInfo> materialsInfo) { // 封装成一个Json文件返回
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (materialsInfo.size() != 0) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
			m.put("result", materialsInfo);
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");
		}
		return m;
	}

	private Map<String, Object> status(Boolean materialsInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (materialsInfo) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");
		}
		return m;
	}

	@RequestMapping("/findAll")
	public @ResponseBody Map<String, Object> getMaterialsInfos() {
		List<MaterialsInfo> materialsInfo = new ArrayList<MaterialsInfo>();
		materialsInfo = materialsService.findAll();
		return status(materialsInfo);
	}

	@RequestMapping("{tunnelName}/findById")
	public @ResponseBody Map<String, Object> getMaterialsInfo(
			@PathVariable String tunnelName) {
		int tunnelId;
		List<TunnelInfo> tunnelInfo=new ArrayList<TunnelInfo>();
		tunnelInfo=tunnelService.findByName(tunnelName);
		tunnelId=tunnelInfo.get(0).getTunnelId();
		List<MaterialsInfo> materialsInfo = new ArrayList<MaterialsInfo>();
		materialsInfo = materialsService.findById(tunnelId);
		return status(materialsInfo);

	}

	@RequestMapping("/addMaterial")
	public @ResponseBody Map<String, Object> addMaterials(
			@RequestBody Map<String, Object> materialsMap) throws ParseException {
		boolean result;
		iter = materialsMap.keySet().iterator();
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		MaterialsInfo materialsInfo = new MaterialsInfo();
		materialsInfo.setTunnelId((Integer) materialsMap.get(list.get(0)));
		materialsInfo.setPersonId((Integer) materialsMap.get(list.get(1)));
		materialsInfo.setMaterialsName((String) materialsMap.get(list.get(2)));
		materialsInfo.setNumber((Integer) materialsMap.get(list.get(3)));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");           //将字符串转换为DATE类型
		Date date=sdf.parse((String) materialsMap.get(list.get(4)));
		materialsInfo.setInputTime(date);
		materialsInfo.setUsePeriod((String) materialsMap.get(list.get(5)));
		result = materialsService.addMaterials(materialsInfo);
		return status(result);
	}

	@RequestMapping("/updateMaterial")
	public @ResponseBody Map<String, Object> updateMaterials(
			@RequestBody Map<String, Object> materialsMap) throws ParseException {
		boolean result;
		iter = materialsMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		MaterialsInfo materialsInfo = new MaterialsInfo();
		materialsInfo.setMaterialsId((Integer) materialsMap.get(list.get(0)));
		materialsInfo.setTunnelId((Integer) materialsMap.get(list.get(1)));
		materialsInfo.setPersonId((Integer) materialsMap.get(list.get(2)));
		materialsInfo.setMaterialsName((String) materialsMap.get(list.get(3)));
		materialsInfo.setNumber((Integer) materialsMap.get(list.get(4)));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");           //将字符串转换为DATE类型
		Date date=sdf.parse((String) materialsMap.get(list.get(5)));
		materialsInfo.setInputTime(date);
		materialsInfo.setUsePeriod((String) materialsMap.get(list.get(6)));
		result = materialsService.updateMaterials(materialsInfo);
		return status(result);
	}

	@RequestMapping("/{materialsId}/deleteMaterial")
	public @ResponseBody Map<String, Object> deleteMaterials(
			@PathVariable int materialsId) {
		boolean result;
		result = materialsService.deleteMaterials(materialsId);
		return status(result);

	}

}
